const app = getApp();

Page({
    data: {
        openId: null,
        studentInfo: {
            parentName: '',
            name: '',
            phone: '',
            email: '',
            sex: 0,
            schoolId: 0,
            gradeId: 0,
            classId: 0,
        },
        hasReg: false,

        sexRange: ['请选择性别', '男', '女'],
        schoolRange: ['请选择学校'],
        gradeRange: ['请选择年级'],
        classRange: ['请选择班级'],
        // , '一班', '二班', '三班', '四班', '五班', '六班', '七班', '八班', '九班', '十班', '十一班', '十二班', '十三班', '十四班', '十五班', '十六班', '十七班', '十八班', '十九班', '二十班', '二十一班', '二十二班', '二十三班', '二十四班', '二十五班', '二十六班', '二十七班', '二十八班', '二十九班', '三十班'
    },
    onLoad: function(options) {
        var that = this;
        that.getRegData(function() {
            app.getStudentInfo(0, function() {
                that.setData({
                    openId: app.globalData.openId,
                    hasReg: app.globalData.hasReg,
                    studentInfo: app.globalData.studentInfo,
                });
                wx.setNavigationBarTitle({
                    title: that.data.hasReg ? '一里学堂 - 学生资料' : '一里学堂 - 学生注册'
                })
            });
        });

        // that.upodateStudentInfo();
    },
    bindStudentInfoChange: function(e) {
        var _json = this.data.studentInfo;
        _json[e.currentTarget.dataset.valuename] = e.detail.value;
        this.setData({
            studentInfo: _json
        })
        // console.log(this.data.studentInfo)
    },
    // upodateStudentInfo:function() {
    //     var that = this;
    //     if (app.globalData.studentInfo) {
    //         that.setData({
    //             studentInfo: app.globalData.studentInfo,
    //             hasReg: app.globalData.hasReg,
    //             openId: app.globalData.openId,
    //         });
    //         // console.log(that.data.hasReg);
    //         // console.log(that.data.studentInfo);
    //         // console.log(that.data.schoolRange);
    //     } else {
    //         setTimeout(function(){
    //             if (app.globalData.studentInfo) {
    //                 that.setData({
    //                     studentInfo: app.globalData.studentInfo,
    //                     hasReg: app.globalData.hasReg,
    //                     openId: app.globalData.openId,
    //                 });
    //             }
    //             // console.log(that.data.hasReg);
    //             // console.log(that.data.studentInfo);
    //             // console.log(that.data.schoolRange);
    //         },1000);
    //     }
    // },
    getRegData: function(callback) {
        var that = this;
        // 获取学校班级年级数据

        //班级 
        var arr_class = ['请选择班级'];
        for (var i = 1; i <= 30; i++) {
            arr_class.push('(' + i + ') 班');
        }
        this.setData({
            classRange: arr_class
        });
        app.showLoading();
        // 学校
        wx.request({
            url: app.api.listSchool + '?time=' + Date.parse(new Date()),
            data: {},
            header: { 'Content-Type': 'application/json' },
            method: 'POST',
            success: function(rs) {
                var list = rs.data.data;
                var arr = ['请选择学校'];
                for (var i in list)
                    arr.push(list[i].name);
                // that.globalData.schoolRange = arr;
                that.setData({
                    schoolRange: arr
                });
                // wx.showModal({
                //     title: '打印',
                //     content: 'schoolRange'+that.data.schoolRange,
                // })
                // console.log(that.data.schoolRange)

                //年级
                wx.request({
                    url: app.api.listGrade + '?time=' + Date.parse(new Date()),
                    data: {},
                    header: { 'Content-Type': 'application/json' },
                    method: 'POST',
                    success: function(rs) {
                        app.hideLoading();
                        var list = rs.data.data;
                        var arr = ['请选择年级'];
                        for (var i in list)
                            arr.push(list[i].name);
                        // that.globalData.gradeRange = arr;
                        that.setData({
                            gradeRange: arr
                        });

                        callback && callback();
                    }
                });
            },
            fail: function(error) {
                app.hideLoading();
                // wx.showModal({
                //     title: '失败',
                //     content: 'error:'+error,
                // })  
            }
        });
    },
    formSubmit: function(e) {
        if (app.status.isLoading) {
            console.log('不能重复提交');
            return;
        }
        app.showLoading(1);
        // console.log(e)
        var warn = ""; //弹框时提示的内容
        var warn_mul = "请完善信息";
        var empty_size = 0;
        if (e.detail.value.parentName == "") {
            warn = empty_size ? warn_mul : "请输入家长姓名！";
            empty_size++;
        }
        if (e.detail.value.name == "") {
            warn = empty_size ? warn_mul : "请输入学生姓名！";
            empty_size++;
        }
        if (e.detail.value.sex == 0) {
            warn = empty_size ? warn_mul : "请选择性别";
            empty_size++;
        }
        if (e.detail.value.phone == "") {
            warn = empty_size ? warn_mul : "请输入手机号！";
            empty_size++;
        }
        if (e.detail.value.email == "") {
            warn = empty_size ? warn_mul : "请输入邮箱！";
            empty_size++;
        }
        if (e.detail.value.schoolId == 0) {
            warn = empty_size ? warn_mul : "请选择学校";
            empty_size++;
        }
        if (e.detail.value.gradeId == 0) {
            warn = empty_size ? warn_mul : "请选择年级";
            empty_size++;
        }
        if (e.detail.value.classId == 0) {
            warn = empty_size ? warn_mul : "请选择班级";
            empty_size++;
        }
        if (!empty_size) {
            if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.phone))) {
                warn = "手机号格式不正确";
            } else if (!(/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(e.detail.value.email))) {
                warn = "邮箱格式不正确！";
            }
        }
        //如果信息填写不完整，弹出输入框
        if (warn) {
            app.hideLoading(1);
            wx.showModal({
                title: '提示',
                content: warn
            })
        } else {
            var that = this;
            // 后面跟的是需要传递到下一个页面的参数
            // wx.navigateTo({
            //     url: '../confirmForest/confirmForest?area=' + e.detail.value.area + '&tel=' + e.detail.value.tel + "&addre=" + that.data.addreRange[e.detail.value.addre] + "&door=" + e.detail.value.door
            // })
            e.detail.value.openId = app.globalData.openId;
            var reg_url = app.api.saveStudent + '?time=' + Date.parse(new Date());
            var save_url = app.api.updateStudent + '?time=' + Date.parse(new Date());
            var is_reg = app.globalData.hasReg ? false : true;
            wx.request({
                url: is_reg ? reg_url : save_url,
                data: e.detail.value,
                header: { 'Content-Type': 'application/json' },
                method: 'POST',
                success: function(rs) {
                    app.hideLoading(1);
                    if (rs.data.status == '0') {
                        wx.showModal({
                            title: '提示',
                            content: (is_reg ? '注册' : '保存') + '成功！',
                            success: function() {
                                /*wx.redirectTo({
                                    url: app.globalData.index_url
                                })*/
                                wx.navigateBack({
                                    delta: 100
                                })
                            }
                        })
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: rs.data.error
                        })
                    }
                }
            });
        }
    }
})